package com.jiangxiaofei.citycloud.activity.village;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.adapter.VillageNewsAdapter;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.JsonBean;
import com.jiangxiaofei.citycloud.bean.VillageNewsBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.VillageConstant;
import com.jiangxiaofei.citycloud.mvp.VillagePersenter;
import com.jiangxiaofei.citycloud.param.VillageListParam;
import com.jiangxiaofei.citycloud.view.ProgressDialogHelper;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by JiangXFei on 2017/8/17.
 */

public class VillageListAcitivity extends BaseActivity implements VillageConstant.VillageListView {
    @InjectView(R.id.village_list_tv_position)
    TextView villageListTvPosition;
    @InjectView(R.id.village_list_tv_type)
    TextView villageListTvType;
    @InjectView(R.id.village_list_lv)
    ListView villageListLv;
    public static final String TYPE = "TYPE";
    private List<VillageNewsBean> newsList = new ArrayList<>();
    private String newsType;
    private OptionsPickerView optionsPickerView;
    private List<String> typeList;
    private ArrayList<JsonBean> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();
    private int page = 1;
    private VillageNewsAdapter newsAdapter;
    private VillageConstant.VillageListPersenter mVillageListPersenter;
    private RefreshLayout refreshLayout;

    @Override
    protected void initView(Bundle savedInstanceState) {
        mContext = this;
        mActivity = this;
        setContentView(R.layout.activity_village_list_layout);
        ButterKnife.inject(this);
        Intent intent = getIntent();
        newsType = intent.getStringExtra(TYPE);
        mVillageListPersenter = new VillagePersenter(mContext, (VillageConstant.VillageListView) mActivity);
        mVillageListPersenter.getVillageList();
        initdate();
        initTitle();
    }

    private void initdate() {
        initRefresh();
        newsAdapter = new VillageNewsAdapter(mContext, newsList);
        villageListLv.setAdapter(newsAdapter);
    }

    private void initRefresh() {
        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
        refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                page++;
                mVillageListPersenter.getVillageList();
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
                newsList.clear();
                page = 1;
                mVillageListPersenter.getVillageList();

            }
        });
    }


    @Override
    protected void setListener() {
        mRightThree.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setCTitle("美丽乡村");
        setLeftOneBg(R.drawable.ic_back);
        setRightThreeBg(R.drawable.ic_publish);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_rightthree:
                //菜单按钮的点击事件
                Intent intent = new Intent(mContext, VillageInfoPublishActivity.class);
                intent.putExtra(StringConstants.PUBLISHTYPE, VillageInfoPublishActivity.PUBLISH);
                mContext.startActivity(intent);
                break;
            default:
                break;
        }
    }

    @OnClick({R.id.village_list_tv_type, R.id.village_list_tv_position})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.village_list_tv_type:
                showPickerViewType(villageListTvType);
                break;
            case R.id.village_list_tv_position:
                showPickerViewPosition(villageListTvPosition);
                break;
            default:
                break;
        }
    }

    public void showPickerViewType(final TextView tv) {
        final String[] stringArray = getResources().getStringArray(R.array.village_menu);
        typeList = new ArrayList<>();
        for (int i = 0; i < stringArray.length; i++) {
            typeList.add(stringArray[i]);
        }
        optionsPickerView = new OptionsPickerView.Builder(mContext, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String tx = typeList.get(options1);
                tv.setText(tx);
            }
        }).setTitleText("类型选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .setOutSideCancelable(false)// default is true
                .build();
        optionsPickerView.setPicker(typeList);
        optionsPickerView.show();
    }

    public void showPickerViewPosition(final TextView tv) {
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(mContext, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
//                String tx = options1Items.get(options1).getPickerViewText() +
//                        options2Items.get(options1).get(options2) +
//                        options3Items.get(options1).get(options2).get(options3);
                String tx = Constants.positions.get(options1);

                tv.setText(tx);
            }
        })

                .setTitleText("地区选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .setOutSideCancelable(false)// default is true
                .build();

//   pvOptions.setPicker(options1Items);//一级选择器
        pvOptions.setPicker(Constants.positions);//二级选择器
//        pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
        pvOptions.show();

    }

    @Override
    public void setData(Object o) {
        List list = (List) o;
        if (list.get(0) instanceof VillageNewsBean) {
            List<VillageNewsBean> nList = (List<VillageNewsBean>) o;
            for (int i = 0; i < nList.size(); i++) {
                newsList.add(nList.get(i));
            }
            newsAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showLoading() {
        ProgressDialogHelper.show(mContext);
    }

    @Override
    public void hideLoading() {

        ProgressDialogHelper.dismissDialog();
    }

    @Override
    public void message(String str) {

    }

    @Override
    public VillageListParam getVillageList() {
        return new VillageListParam(String.valueOf(page), newsType);
    }
}
